/**
 * 需要创建新的节点，统一在本js内进行配置
 */
import {getRandNodeId,getRandNodeStart} from "../../../../../../../utils/ProcessUtil.js";
import {defineAsyncComponent} from "vue";

const Nodes = import.meta.glob('./node/*.vue')
const NodeConfigs = import.meta.glob('./config/*.vue')

//批量导出所有的component下面的表单组件
export const NodeComponents = {}
Object.keys(Nodes).forEach((key) => {
  const name = key.replace(/^.+\/([^/]+)\.vue$/, '$1')
  NodeComponents[name] = defineAsyncComponent(Nodes[key])
})

export const NodeComponentConfigs = {}
Object.keys(NodeConfigs).forEach((key) => {
  const name = key.replace(/^.+\/([^/]+)\.vue$/, '$1')
  NodeComponentConfigs[name] = defineAsyncComponent(NodeConfigs[key])
})

const createGateway = (type) => {
  return {
    id: getRandNodeId() + '_fork',
    type: 'GATEWAY',
    name: '网关节点',
    parentId: null,
    childId: null,
    props: {
      type: type,
      branch: [
        //默认创建俩分支
        branchNode[type].createSelf(1),
        branchNode[type].createSelf()
      ]
    },
    branch: [[], []] //默认要创建2个空分支
  }
}
const createGateway1 = (type) => {
  return {
    id: getRandNodeId() + '_fork',
    type: 'GATEWAY',
    name: '网关节点',
    parentId: null,
    childId: null,
    props: {
      type: type,
      branch: [
        //默认创建俩分支
        CONDITION.createSelf(1),
        CONDITION.createSelf()
      ]
    },
    branch: [[], []] //默认要创建2个空分支
  }
}
//定义分支子节点
const branchNode = {
  PARALLEL: {
    name: '并行节点',
    icon: 'Operation',
    color: '#718dff',
    //创建自身
    createSelf(i) {
      return {
        id: getRandNodeId(),
        type: 'PARALLEL',
        name: '并行节点' + (i ? i: 2),
        parentId: null,
        childId: null,
        props: {
          id:0,//主键
          flowId:0,//流程
          name:'',//节点名称
          code:'',//节点编码
          unitId:'',
          passNodeId:'',//下一节点
          jumpNodeCode:'',//下一节点：不通过节点
          roleId:'',//角色ID
          timeLimit:'',//规定完成时间天数
          completeTime:'',//规定完成时间
          nodeCategory:'1',//节点分类：1-保前，2-保后
          crossDept:'0',//是否跨部门：0-否，1-是
          batchApprove:'0',//是否批量审批：0-否，1-是
          isCustomOperator:'0',//是否自选操作人员：0-否，1-是
          isPreRelease:'0',
          isApplyRefund:'0',
          isLastInspect:'0',
          isAudit:'0',
          nodeIdent:'',//节点状态标识
          canBack:'1',//是否可回退
          skipStop:'0',//是否跳过暂停
          handler:'',
          isExpand:'0',
          msgTempIds:[],
        }
      }
    },
    create() {
      return createGateway('PARALLEL')
    }
  },
}

//开始节点
const START = {
  create() {
    return {
      id: getRandNodeStart(),
      type: 'START',
      name: '业务进件',
      parentId: null,
      childId: null,
      props: {
        unitId:'',
        nodeCategory:'1',
      },
    }
  }
}
//审批节点
const APPROVAL = {
  name: '添加节点',
  icon: 'Stamp',
  color: '#EF994F',
  create() {
    return {
      id: getRandNodeId(),
      type: 'APPROVAL',
      name: '节点',
      parentId: null,
      childId: null,
      props: {
        id:0,//主键
        flowId:0,//流程
        name:'',//节点名称
        code:'',//节点编码
        unitId:'',
        passNodeId:'',//下一节点
        jumpNodeCode:'',//下一节点：不通过节点
        roleId:'',//角色ID
        timeLimit:'',//规定完成时间天数
        completeTime:'',//规定完成时间
        nodeCategory:'1',//节点分类：1-保前，2-保后
        crossDept:'0',//是否跨部门：0-否，1-是
        batchApprove:'0',//是否批量审批：0-否，1-是
        isCustomOperator:'0',//是否自选操作人员：0-否，1-是
        isPreRelease:'0',
        isApplyRefund:'0',
        isLastInspect:'0',
        fileTemplateIds:[],
        customOperator:'',
        isAudit:'0',
        nodeIdent:'',//节点状态标识
        canBack:'1',//是否可回退
        skipStop:'0',//是否跳过暂停
        handler:'',
        isExpand:'0',
        msgTempIds:[],
      },
    }
  }
}
//触发节点
const TRIGGER = {
  name: '触发器',
  icon: 'Promotion',
  color: '#5994F3',
  create() {
    return {
      id: getRandNodeId(),
      type: 'TRIGGER',
      name: '触发器',
      parentId: null,
      childId: null,
      props: {
        id:0,//主键
        flowId:0,//流程
        name:'',//节点名称
        code:'',//节点编码
        unitId:'',
        passNodeId:'',//下一节点
        triggerType:'1',
        jumpNodeCode:'',//下一节点：不通过节点
        nodeCategory:'1',
        roleId:'',//角色ID
        timeLimit:'',//规定完成时间天数
        completeTime:'',//规定完成时间
        nodeCategory:'1',//节点分类：1-保前，2-保后
        crossDept:'0',//是否跨部门：0-否，1-是
        batchApprove:'0',//是否批量审批：0-否，1-是
        isCustomOperator:'0',//是否自选操作人员：0-否，1-是
        fileTemplateIds:[],
        customOperator:'',
      },
    }
  }
}
//条件分支
const CONDITION={
  name: '条件节点',
    icon: 'Operation',
    color: '#718dff',
    //创建自身
    createSelf(i) {
      return {
        id: getRandNodeId(),
        type: 'CONDITION',
        name: '条件节点' + (i ? i: 2),
        parentId: null,
        childId: null,
        props: {
          id:0,//主键
          flowId:0,//流程
          name:'',//节点名称
          code:'',//节点编码
          nodeCategory:'1',
          expressions:[
            {
              type:1,
              enable:'1',
              k:'',
              v:"",
              v1:'',
            }
          ],
          // expression:{
          //   conditionOne:{
          //     ruleOne:'',
          //      numOne:'',
          //      ruleTwo:'',
          //      numTwo:'',
          //      status:'1',//0是关，1是开
          //   },
          //   conditionTwo:{
          //     ruleOne:'',
          //     numOne:'',
          //     status:'1',//0是关，1是开
          //   },
          //   conditionThree:{
          //     ruleOne:'',
          //     numOne:'',
          //     status:'1',//0是关，1是开
          //     isSecurity:'1',//0是关，1是开
          //   },
          // },//条件判断表达式
        },
      }
    },
    create() {
      return createGateway1('PARALLEL')
    }
  
}
//延迟节点
const  DELAY={
  name: '延迟等待',
  icon: 'Promotion',
  color: '#5994F3',
  create() {
    return {
      id: getRandNodeId(),
      type: 'DELAY',
      name: '延迟等待',
      parentId: null,
      childId: null,
      props: {
        id:0,//主键
        flowId:0,//流程
        name:'',//节点名称
        code:'',//节点编码
        timeLimit:'',
        nodeCategory:'1'
      },
    }
  }
}

export default {
  //审批节点
  APPROVAL: APPROVAL,
  START: START, 
  //注入分支节点定义
  ...branchNode,
  TRIGGER:TRIGGER,
  CONDITION:CONDITION,
  DELAY:DELAY
}


